package org.clearjava.ioc.impl;

import java.util.HashMap;
import java.util.Map;

import org.clearjava.ioc.face.BeanMap;

public class SimpleBeanMap implements BeanMap {
	
	private Map<String,Object> beansByClass = new HashMap<String,Object>();
	
	private Map<String,Class<?>> classByName = new HashMap<String,Class<?>>();
	
	private Map<Class<?>,Map<String,String>> classes = new HashMap<Class<?>,Map<String,String>>();
	
	@Override
	public Object getByName(String name) {
		Class<?> claz = classByName.get(name);
		if(claz!=null){
			Object obj = beansByClass.get(claz);
			if(obj==null) {
				try{
					obj = claz.newInstance();
				}catch(Exception e){
					e.printStackTrace();
					return null;
				}
			}
			return obj;
		}else{
			return null;
		}
	}

	@Override
	public Object getByClass(Class<?> claz) {if(claz!=null){
		Object obj = beansByClass.get(claz);
		if(obj==null) {
			try{
				obj = claz.newInstance();
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}
		return obj;
		}else{
			return null;
		}
	}

	@Override
	public void set(String name, Class<?> claz, Map<String, String> config) {
		classes.put(claz, config);
		classByName.put(name, claz);
	}

	@Override
	public void set(Class<?> claz, Map<String, String> config) {
		classes.put(claz, config);
	}

	@Override
	public void set(Class<?> claz) {
		classes.put(claz, null);
		
	}

	@Override
	public void set(String name, Class<?> claz) {
		classes.put(claz, null);
		classByName.put(name, claz);
	}
	
}
